<?php defined('BASEPATH') OR exit('No direct script access allowed');

class Resolutions_private extends wsFrontend
{

    public function __construct()
    {
        parent::__construct();
        if (strpos(strtolower($this->uri->uri_string()), 'resolutions_private') !== false) {
            redirect(base_url());
        }
        $this->db->cache_set_path(dirname($this->db->cachedir) . '/Resolutions_page/');
    }

    public function index($width, $height)
    {
        $data = array();
        $data['size'] = $filter['size'] = array('width' => $width, 'height' => $height);

        $this->load->model(array('wallpaper_model', 'resolution_model'));

        $this->db->custom_seqgment('resolutions', 'index');
        $resolutions = $this->res_dropdown($this->resolution_model->get_AllResolutions(array(
            'status_rg' => 1,
            'status' => 1,
        )));
        $this->db->reset_custom_seqgment();

        $index = $width . 'x' . $height;
        if (!array_key_exists($index, $resolutions))
            redirect();

        $this->tpl->selected_resolution = array('width' => $width, 'height' => $height);


        if (isset($_GET['sort']))
            $filter['sort'] = $_GET['sort'];

        if (isset($_GET['time']))
            $filter['time'] = $_GET['time'];
        $filter['bypath'] = false;

        $filter['show_disabled'] = FALSE;
        $filter['per_page'] = $config['per_page'] = $this->config->item('items_per_page');
        $page_number = (int)$this->input->get('page');
        $filter['offset'] = ($page_number ? ($page_number - 1) * $config['per_page'] : 0);
        $filter['bypath'] = false;

        $data['total_count'] = $this->wallpaper_model->getWallpapers($filter + array('count' => true));

        //Initialize Pagination
        $config['base_url'] = $data['url'] = current_url();
        $config['total_rows'] = $data['total_count'];
        $config['custom_query_string'] = TRUE;
        $config['page_query_string'] = TRUE;
        $config['query_string_segment'] = 'page';

        if (!empty($_GET)) {
            $get_method = $this->frontend_helper->get_query_string(array('page'));
            $config['prefix'] = ($get_method ? $get_method . '&' : '?') . 'page=';
            $config['first_url'] = $config['base_url'] . $get_method;
        } else {
            $config['prefix'] = '?page=';
        }

        $this->load->library('pagination');
        $this->pagination->initialize($config);
        $data['pagination'] = $this->pagination->create_links();
        $data['wallpapers'] = $this->wallpaper_model->getWallpapers($filter);



        $SearchReplace = array(
            '{width}' => $width,
            '{height}' => $height,
            '{resolution_name}' => $resolutions[$index]->resolution_name,
        );
        

        $this->tpl->meta ['title'] = $this->metadata->get_metadata('_resolution_page_title', 'resolutions_page', $SearchReplace, false, $page_number);
        $this->tpl->meta ['description'] = $this->metadata->get_metadata('_resolution_page_description', 'resolutions_page', $SearchReplace, false, $page_number);
        $this->tpl->meta ['keywords'] = $this->metadata->get_metadata('_resolution_page_keywords', 'resolutions_page', $SearchReplace, false, $page_number);


        $this->tpl->contents [] = $this->tpl->fetch('contents/resolutions_page', $data);
        $this->tpl->render('layouts/home');
    }

    private function res_dropdown($data)
    {
        if (!$data)
            return array();
        $return = array();
        foreach ($data as $d) {
            $return[$d->resolution_width . 'x' . $d->resolution_height] = $d;
        }
        return $return;
    }
}
